package org.jsola.hr.service.impl;

import org.jsola.common.StrKit;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsBankDetailDAO;
import org.jsola.hr.dto.TransferAccountsBankDetailAddDTO;
import org.jsola.hr.dto.TransferAccountsBankDetailUpdateDTO;
import org.jsola.hr.entity.TransferAccountsBankDetailDO;
import org.jsola.hr.query.TransferAccountsBankDetailQuery;
import org.jsola.hr.service.ITransferAccountsBankDetailService;
import org.jsola.hr.vo.TransferAccountsBankDetailListVO;
import org.jsola.hr.vo.TransferAccountsBankDetailVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账明细(平安银行)
 *
 * @author zhr
 */
@Service("hrTransferAccountsBankDetailServiceImpl")
public class TransferAccountsBankDetailServiceImpl implements ITransferAccountsBankDetailService {

    @Autowired
    private ITransferAccountsBankDetailDAO transferAccountsBankDetailDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsBankDetailVO save(TransferAccountsBankDetailAddDTO transferAccountsBankDetailAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsBankDetailDO transferAccountsBankDetailDO = transferAccountsBankDetailAddDTO.to(TransferAccountsBankDetailDO.class);
        // 保存
        transferAccountsBankDetailDO = save(transferAccountsBankDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsBankDetailDO.to(TransferAccountsBankDetailVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsBankDetailUpdateDTO transferAccountsBankDetailUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsBankDetailDO transferAccountsBankDetailDO = transferAccountsBankDetailUpdateDTO.to(TransferAccountsBankDetailDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsBankDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsBankDetailIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsBankDetailIds);
    }


    @Override
    public TransferAccountsBankDetailVO selectById(String transferAccountsBankDetailId, String siteId) {
        TransferAccountsBankDetailDO transferAccountsBankDetailDO = selectDOById(transferAccountsBankDetailId, siteId);
        if (transferAccountsBankDetailDO == null) {
            return null;
        }
        return transferAccountsBankDetailDO.to(TransferAccountsBankDetailVO.class);
    }

    @Override
    public List<TransferAccountsBankDetailListVO> select(TransferAccountsBankDetailQuery transferAccountsBankDetailQuery, String siteId) {
        List<TransferAccountsBankDetailDO> transferAccountsBankDetailDOList = selectDO(transferAccountsBankDetailQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsBankDetailDOList)) {
            return transferAccountsBankDetailDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsBankDetailDOList.stream()
                .map(transferAccountsBankDetailDO -> transferAccountsBankDetailDO.to(TransferAccountsBankDetailListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsBankDetailQuery transferAccountsBankDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsBankDetailQuery,siteId);
        return transferAccountsBankDetailDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsBankDetailListVO> selectPage(TransferAccountsBankDetailQuery transferAccountsBankDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsBankDetailQuery,siteId);
        Page<TransferAccountsBankDetailDO> page = transferAccountsBankDetailDAO.selectPageByExample(example,
                transferAccountsBankDetailQuery.getPageNo(),
                transferAccountsBankDetailQuery.getPageSize());

        return page.to(TransferAccountsBankDetailListVO.class);
    }

    @Override
    public TransferAccountsBankDetailDO selectDOById(String transferAccountsBankDetailId, String siteId) {
        return listById(transferAccountsBankDetailId, siteId);
    }

    @Override
    public List<TransferAccountsBankDetailDO> selectDO(TransferAccountsBankDetailQuery transferAccountsBankDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsBankDetailQuery,siteId);
        return transferAccountsBankDetailDAO.selectByExample(example);
    }

    @Override
    public TransferAccountsBankDetailDO selectDOBySThirdVoucher(String sThirdVoucher, String siteId) {
        if (StrKit.isEmpty(sThirdVoucher)) {
            return null;
        }
        Example example = new Example(TransferAccountsBankDetailDO.class);
        example.and()
                .andEqualTo("sThirdVoucher", sThirdVoucher)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return transferAccountsBankDetailDAO.selectOneByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsBankDetailQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsBankDetailQuery transferAccountsBankDetailQuery, String siteId) {
        Example example = new Example(TransferAccountsBankDetailDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsBankDetailQuery.getOrders());
        return example;
    }
}




